package com.cskaoyan.controller.backgroundsystemcontroller;


import com.cskaoyan.bean.bo.CouponListBo;
import com.cskaoyan.bean.bo.CouponListuserBo;
import com.cskaoyan.bean.pojo.Coupon;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.CouponListVo;
import com.cskaoyan.bean.vo.CouponListuserVo;
import com.cskaoyan.service.PromotionService;
import com.cskaoyan.utils.InvalidatedParamsUtil;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: lsy
 * @class: CouponController
 */
@RestController
@RequestMapping("/admin/coupon")
public class CouponController {

    @Autowired
    PromotionService promotionService;

    /**
     * @author: lsy
     * @title: list
     * @description: 分页查询优惠券信息
     * @param: [baseQueryBo]
     * @return: com.cskaoyan.bean.vo.WxBaseRespVo
     */
    @RequiresPermissions(value = {"*","admin:coupon:list"},logical = Logical.OR)
    @GetMapping("list")
    public BaseRespVo list(CouponListBo couponListBo) {
        CouponListVo couponListVo = promotionService.queryCoupons(couponListBo);
        return BaseRespVo.ok(couponListVo);
    }

    /**
     * @author: lsy
     * @title: create
     * @param: [couponCreateBo]
     * @return: com.cskaoyan.bean.vo.WxBaseRespVo
     */
    @PostMapping("create")
    @RequiresPermissions(value = {"*","admin:coupon:create"},logical = Logical.OR)
    public BaseRespVo create(@RequestBody @Validated Coupon coupon, BindingResult result) {
        if (result.hasErrors()) {
            return InvalidatedParamsUtil.resErrorVo(result);
        }
        coupon.setDeleted(false);
        coupon.setAddTime(new Date());
        promotionService.addCoupon(coupon);
        return BaseRespVo.ok(coupon);
    }

    /**
     * @author: lsy
     * @title: update
     * @description: 编辑优惠券信息
     * @param: [coupon]
     * @return: com.cskaoyan.bean.vo.WxBaseRespVo
     */
    @PostMapping("update")
    @RequiresPermissions(value = {"*","admin:coupon:update"},logical = Logical.OR)
    public BaseRespVo update(@RequestBody @Validated Coupon coupon, BindingResult result) {
        if (result.hasErrors()) {
            return InvalidatedParamsUtil.resErrorVo(result);
        }
        promotionService.changeCoupon(coupon);
        return BaseRespVo.ok();
    }

    @PostMapping("delete")
    @RequiresPermissions(value = {"*","admin:coupon:delete"},logical = Logical.OR)

    public BaseRespVo delete(@RequestBody @Validated Coupon coupon, BindingResult result) {
        if (result.hasErrors()) {
            return InvalidatedParamsUtil.resErrorVo(result);
        }
        Coupon coupon1 = new Coupon();
        coupon1.setId(coupon.getId());
        coupon1.setDeleted(true);
        promotionService.changeCoupon(coupon1);
        return BaseRespVo.ok();
    }

    /**
     * @author: lsy
     * @title: read
     * @description: 根据id查找优惠券详情
     * @param: [id]
     * @return: com.cskaoyan.bean.vo.WxBaseRespVo
     */
    @GetMapping("read")
    @RequiresPermissions(value = {"*","admin:coupon:read"},logical = Logical.OR)
    public BaseRespVo read(int id) {
        Coupon coupon = promotionService.queryCoupon(id);
        return BaseRespVo.ok(coupon);
    }

    /**
     * @author: lsy
     * @title: listuser
     * @description: 根据优惠券id couponId，用户id userId，使用状态 status查询所有拥有该优惠券的用户信息
     * page: 1
     * limit: 20
     * couponId: 2
     * userId: 3
     * status: 0
     * sort: add_time
     * order: desc
     * @param: [couponListuserBo]
     * @return: com.cskaoyan.bean.vo.WxBaseRespVo
     */
    @GetMapping("listuser")
    @RequiresPermissions(value = {"*","admin:coupon:listuser"},logical = Logical.OR)
    public BaseRespVo listuser(CouponListuserBo couponListuserBo) {
        CouponListuserVo couponListuserVo = promotionService.queryUsersByCouponIdAndUserIdAndStatus(couponListuserBo);
        return BaseRespVo.ok(couponListuserVo);
    }
}
